package com.vectquant.lwwl_service01.controller;



import com.vectquant.lwwl_service01.model.User;
import com.vectquant.lwwl_service01.service.UserService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
import java.util.HashMap;

/**
 * 认证控制器，处理登录和注册请求
 * @RestController 表示这是一个REST控制器
 * @RequestMapping 定义基础路径
 * @CrossOrigin 允许跨域请求
 */
@RestController
@RequestMapping("/api/auth")
@CrossOrigin(origins = "*")

public class AuthController {
    private final UserService userService;
    public AuthController(UserService userService){
        this.userService = userService;
    }
    /**
     * 用户注册接口
     * @param requestData 包含code,encryptedData,iv的请求数据
     * @return 登录结果
     */
    @PostMapping("/login")
    public ResponseEntity<?> login( @RequestBody Map<String,String> requestData){
        try {
            String code = requestData.get("code");
//            String encryptedData = requestData.get("encryptedData");
//            String iv = requestData.get("iv");
            String phoneCode = requestData.get("phoneCode");
//            调用服务层注册用户
            User user = userService.loginOrRegister(code, phoneCode);
//            创建响应数据(不包含密码)
            Map<String,Object> response = new HashMap<>();
            response.put("message","登录成功");
            response.put("data",creatUserResponse(user));
            response.put("code",200);
            return ResponseEntity.ok(response);
        }catch (Exception e){
//            处理异常请求
            Map<String,Object> error = new HashMap<>();
            error.put("code",500);
            error.put("message","登陆失败：" + e.getMessage());
            return ResponseEntity.status(500).body(error);
        }
    }
    /**
     * 新增接口：获取手机号
     * @param requesData 包含phoneCode的请求数据
     * @return 手机号信息
     */
    @PostMapping("/getPhoneNumber")
    public ResponseEntity<?> getPhoneNumber (@RequestBody Map<String,String> requesData){
        try{
            String phoneCode = requesData.get("phoneCode");
            String phoneNumber = userService.getPhoneNumber(phoneCode);

            Map<String,Object> response = new HashMap<>();
            response.put("message","获取手机号成功");
            response.put("data",phoneNumber);
            response.put("code",200);
            return  ResponseEntity.ok(response);

        }catch (Exception e){
            Map<String,Object> error = new HashMap<>();
            error.put("code",500);
            error.put("message","获取手机号失败："+e.getMessage());
            return ResponseEntity.status(500).body(error);
        }
    }
    /**
     *获取用户信息接口
     * @param openid 用户openid
     * @return 用户信息
     */
    @GetMapping("/userinfo")
    public ResponseEntity<?> getUserInfo( @RequestParam String openid){
        try{
            User user = userService.getUserByOpenid(openid);
//            创建响应数据(不包含密码)
            Map<String,Object> response = new HashMap<>();
            response.put("message","获取成功");
            response.put("data",creatUserResponse(user));
            response.put("code",200);

            return ResponseEntity.ok(response);
        }catch (Exception e){
//            处理登录失败情况
            Map<String,Object> error = new HashMap<>();
            error.put("code", 500);
            error.put("message","获取用户信息失败" + e.getMessage());
            return ResponseEntity.status(500).body(error);
        }
    }
    /**
     * 更新用户信息接口
     * @param user 用户信息
     * @return 更新结果
     */
    @PostMapping("/updateUser")
    public ResponseEntity<?> updateUser(@RequestBody User user){
        try{
            User updateUser = userService.updateUser(user);
            Map<String,Object> response = new HashMap<>();
            response.put("code",200);
            response.put("message","更新成功");
            response.put("data",creatUserResponse(updateUser));

            return ResponseEntity.ok(response);

        }catch (Exception e){
            Map<String,Object> error = new HashMap<>();
            error.put("code",500);
            error.put("message","更新用户信息失败：" + e.getMessage());
            return ResponseEntity.status(500).body(error);
        }
    }
    /**
     * 创建用户响应数据
     * @param user用户对象
     * @return 过滤后的用户信息
     */
    private Map<String,Object> creatUserResponse(User user){
        Map<String, Object> userResponse = new HashMap<>();
        userResponse.put("id",user.getId());
        userResponse.put("openid",user.getOpenid());
        userResponse.put("phone",user.getPhone());
        userResponse.put("nickname",user.getNickname());
        userResponse.put("avatar",user.getAvatar());
        userResponse.put("creatTime",user.getCreatTime());
        userResponse.put("lastLoginTime",user.getLastLoginTime());
        return userResponse;
    }
}
